<?php
use \Firebase\JWT\JWT;

$allow = b2_get_option('normal_product','product_allow');
if(!$allow){
	wp_safe_redirect(B2_HOME_URI.'/404');
	exit;
}

$post_id = 0;

//category dropdown settings
$cats = b2_get_option('normal_write','write_cats');
$default_cats = b2_get_option('normal_write','write_cats_default');
$p_cat = [];
$cats = $cats ? $cats : array();
$arr_arr = array();
if(!empty($cats)){
    foreach ($cats as $k => $v) {
        $cat = get_term_by('slug', $v,'category');
        if($cat){
            $arr_arr[] = array(
                'id'=>$cat->term_id,
                'name'=>$cat->name
            );
            $p_cat[] = $cat->term_id;
        }
    }
}

/**
 * 投稿产品页面
 */
get_header();

?>
    <div class="b2-single-content wrapper">
       <div id="write" class="content-area write-page">
           <main id="main" class="site-main box b2-radius">
               <div id="write-head">
                   <div class="write-thumb" :style="postData.thumb ? 'min-height:auto' : ''">
                       <label @click.stop="showImgLib" v-if="!postData.thumb && !locked">
                           <svg class="b2 b2--Camera WriteCover-uploadIcon" fill="currentColor" viewBox="0 0 24 24" width="42" height="42"><path d="M20.094 6S22 6 22 8v10.017S22 20 19 20H4.036S2 20 2 18V7.967S2 6 4 6h3s1-2 2-2h6c1 0 2 2 2 2h3.094zM12 16a3.5 3.5 0 1 1 0-7 3.5 3.5 0 0 1 0 7zm0 1.5a5 5 0 1 0-.001-10.001A5 5 0 0 0 12 17.5zm7.5-8a1 1 0 1 0 0-2 1 1 0 0 0 0 2z" fill-rule="evenodd"></path></svg>
                           <p><?php echo __('添加题图','b2'); ?></p>
                       </label>
                       <img :src="postData.thumb" v-if="postData.thumb" />
                       <div class="write-reset-thumb" v-if="postData.thumb && !locked" v-cloak>
                           <label class="text button" @click.stop="showImgLib">
                               <svg class="b2 b2--Camera" fill="currentColor" viewBox="0 0 24 24" width="24" height="24"><path d="M20.094 6S22 6 22 8v10.017S22 20 19 20H4.036S2 20 2 18V7.967S2 6 4 6h3s1-2 2-2h6c1 0 2 2 2 2h3.094zM12 16a3.5 3.5 0 1 1 0-7 3.5 3.5 0 0 1 0 7zm0 1.5a5 5 0 1 0-.001-10.001A5 5 0 0 0 12 17.5zm7.5-8a1 1 0 1 0 0-2 1 1 0 0 0 0 2z" fill-rule="evenodd"></path></svg>
                           </label>
                           <button class="text" @click.stop="postData.thumb = ''">
                               <svg class="b2 b2--TrashOutline" fill="currentColor" viewBox="0 0 24 24" width="24" height="24"><path d="M16.213 18.638h-7.83V8.085H7.021v10.553c0 .751.611 1.362 1.362 1.362h7.83c.75 0 1.361-.61 1.361-1.362V8.085h-1.361v10.553zM15.19 5.362c0-.751-.61-1.362-1.361-1.362h-3.064c-.751 0-1.362.61-1.362 1.362v.68H6.766S6 6 6 6.715c0 .714.766.69.766.69H17.89s.705 0 .705-.688c0-.689-.705-.673-.705-.673h-2.7v-.681zm-1.361.68h-3.064v-.68h3.064v.68zm-3.745 3.064v8.17h1.362v-8.17h-1.362zm3.064 0v8.17h1.362v-8.17h-1.362z" fill-rule="evenodd"></path></svg>
                           </button>
                       </div>
                       <div class="thumb-upload-loading" v-if="locked" v-cloak>
                           <span class="button text b2-loading"></span>
                       </div>
                   </div>
                   <?php if(!empty($arr_arr)){ ?>
                       <div class="write-select-row-top">
                           <div class="write-select-title"><?php echo __('分类','b2'); ?></div>
                           <div class="cat-picked">
                               <span v-for="item in postData.cats" v-text="catPicked(item)" @click.stop="removeCat(item)" v-if="item"></span>
                           </div>
                           <div class="write-select-box">
                               <?php
                               $dropdown_args = array(
                                   'hide_empty'       => 0,
                                   'hide_if_empty'    => false,
                                   'taxonomy'         => 'category',
                                   'name'             => 'parent',
                                   'orderby'          => 'name',
                                   'hierarchical'     => true,
                                   'show_option_none' => __( '分类','b2'),
                                   'include'=>$p_cat
                               );

                               $dropdown_args = apply_filters( 'taxonomy_parent_dropdown_args', $dropdown_args, 'category', 'new' );

                               wp_dropdown_categories( $dropdown_args );
                               ?>
                           </div>
                       </div>
                   <?php } ?>
                   <div class="write-select-row-top">
                       <textarea id="write-textarea" class="write-textarea" ref="writeTitle" rows="1" placeholder="<?php echo __('标题','b2'); ?>"></textarea>
                   </div>
               </div>
               <div id="b2-editor-box">
                   <textarea id="mytextarea"></textarea>
                   <div :class="['trix-dialog trix-dialog--images modal',{'show-modal':showImageBox}]" v-cloak>
                       <div class="trix-dialog__link-fields box">
                           <div class="trix-dialog-title" v-if="!thumbPicked"><?php echo __('插入图片','b2'); ?></div>
                           <div class="trix-dialog-title" v-else><?php echo __('设置题图','b2'); ?></div>
                           <span class="close-button" @click.stop="close('image')">×</span>
                           <div class="image-table">
                               <div :class="imgTable == 'upload' ? 'picked' : ''" @click.stop="imgTable = 'upload'"><?php echo __('上传图片','b2'); ?></div>
                               <div :class="imgTable == 'lib' ? 'picked' : ''" @click.stop="imgTable = 'lib'"><?php echo __('我的图片','b2'); ?></div>
                               <div :class="imgTable == 'link' ? 'picked' : ''" @click.stop="imgTable = 'link'" v-if="!thumbPicked"><?php echo __('外链图片','b2'); ?></div>
                           </div>
                           <label class="image-upload-box" v-if="imgTable == 'upload'">
                               <div class="">
                                   <svg class="b2 b2--BackToTop" fill="currentColor" viewBox="0 0 24 24" width="70" height="70"><path d="M16.036 19.59a1 1 0 0 1-.997.995H9.032a.996.996 0 0 1-.997-.996v-7.005H5.03c-1.1 0-1.36-.633-.578-1.416L11.33 4.29a1.003 1.003 0 0 1 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.005z"></path></svg>
                                   <p><?php echo __('请选择要上传的图片','b2'); ?></p>
                               </div>
                               <input type="file" class="b2-hidden-always" accept="image/jpg,image/jpeg,image/png,image/gif" @change="fileUpload($event,'image')" multiple="multiple">
                           </label>
                           <div class="trix-dialog-image-box" v-if="imgTable == 'lib'">
                               <div class="" v-if="imageList.length > 0">
                                   <ul class="editor-images-list">
                                       <li v-for="item in imageList" @click.stop="item.thumb ? picked('image',item.att_url) : ''" :class="(imagePicked.indexOf(item.att_url) !== -1 && !thumbPicked) || (thumb == item.att_url && thumbPicked) ? 'picked' : ''">
                                           <div class="editor-image">
                                               <span v-if="!item.thumb" class="b2-loading button text"></span>
                                               <img :src="item.thumb" v-if="item.thumb">
                                           </div>
                                       </li>
                                   </ul>
                                   <div class="image-nav" v-if="imagePages > 0">
                                       <button class="text" :disabled="locked.pre || paged == 1 ? true : false" @click.stop="per('image')"><?php echo __('上一页','b2'); ?></button>
                                       <button class="text" :disabled="locked.next || paged == imagePages ? true : false" @click.stop="next('image')"><?php echo __('下一页','b2'); ?></button>
                                   </div>
                               </div>
                               <div class="editor-file-none" v-else>
                                   <?php echo __('您未上传过图片','b2'); ?>
                               </div>
                           </div>
                           <div class="trix-dialog-input-box" v-if="imgTable == 'link'">
                               <div class="trix-dialog-input input-textarea">
                                   <textarea type="text" name="imageLink" class="trix-input trix-input--dialog" id="imageLink"></textarea>
                               </div>
                               <p class="dialog-desc"><?php echo __('支持多张外链图片，每个图片连接占一行','b2'); ?></p>
                           </div>
                           <div class="trix-button-group" v-show="imgTable != 'upload'">
                               <button  class="empty" @click.stop="close('image')"><?php echo __('取消','b2'); ?></button>
                               <button @click.stop="insert('image')"  v-if="!thumbPicked"><?php echo __('插入','b2'); ?></button>
                               <button @click.stop="setThumb"  v-else><?php echo __('设置','b2'); ?></button>
                           </div>
                       </div>
                   </div>
                   <div :class="['trix-dialog trix-dialog--video modal',{'show-modal':showVideoBox}]" v-cloak>
                       <div class="trix-dialog__link-fields box">
                           <div class="trix-dialog-title"><?php echo __('插入视频','b2'); ?></div>
                           <span class="close-button" @click.stop="close('video')">×</span>
                           <div class="image-table">
                               <div :class="videoTable == 'upload' ? 'picked' : ''" @click.stop="videoTable = 'upload'"><?php echo __('上传视频','b2'); ?></div>
                               <div :class="videoTable == 'lib' ? 'picked' : ''" @click.stop="videoTable = 'lib'"><?php echo __('我的视频','b2'); ?></div>
                               <div :class="videoTable == 'link' ? 'picked' : ''" @click.stop="videoTable = 'link'"><?php echo __('外链视频','b2'); ?></div>
                               <div :class="videoTable == 'html' ? 'picked' : ''" @click.stop="videoTable = 'html'"><?php echo __('内嵌视频','b2'); ?></div>
                           </div>
                           <label class="image-upload-box" v-if="videoTable == 'upload'">
                               <div class="">
                                   <svg class="b2 b2--BackToTop" fill="currentColor" viewBox="0 0 24 24" width="70" height="70"><path d="M16.036 19.59a1 1 0 0 1-.997.995H9.032a.996.996 0 0 1-.997-.996v-7.005H5.03c-1.1 0-1.36-.633-.578-1.416L11.33 4.29a1.003 1.003 0 0 1 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.005z"></path></svg>
                                   <p><?php echo __('请选择要上传的视频','b2'); ?></p>
                               </div>
                               <input type="file" class="b2-hidden-always" accept="video/mp4,video/x-ms-asf,video/x-ms-wmv,video/x-ms-wmx,video/x-ms-wm,video/avi,video/divx,video/x-flv,video/quicktime,video/mpeg,video/ogg,video/webm,video/x-matroska,video/3gpp,video/3gpp2" @change="fileUpload($event,'video')">
                           </label>
                           <div class="trix-dialog-video-box" v-if="videoTable == 'lib'">
                               <div class="" v-if="videoList.length > 0">
                                   <ul class="editor-images-list">
                                       <li v-for="item in videoList" @click.stop="item.att_url ? picked('video',item.att_url) : ''" :class="videoPicked && videoPicked.indexOf(item.att_url) !== -1 ? 'picked' : ''">
                                           <div class="editor-image">
                                               <span v-if="!item.att_url" class="b2-loading button text"></span>
                                               <video muted :src="item.att_url" v-if="item.att_url" @mouseenter="videoplay($event,'play')" @mouseleave="videoplay($event,'stop')"></video>
                                           </div>
                                       </li>
                                   </ul>
                                   <div class="image-nav" v-if="videoPages > 0">
                                       <button class="text" :disabled="locked.pre || paged == 1 ? true : false" @click.stop="per('video')"><?php echo __('上一页','b2'); ?></button>
                                       <button class="text" :disabled="locked.next || paged == videoPages ? true : false" @click.stop="next('video')"><?php echo __('下一页','b2'); ?></button>
                                   </div>
                               </div>
                               <div class="editor-file-none" v-else>
                                   <?php echo __('您未上传过视频','b2'); ?>
                               </div>
                           </div>
                           <div v-if="videoTable == 'link'">
                               <div class="trix-dialog-input-box">
                                   <div class="trix-dialog-input">
                                       <label for="videoLink"><?php echo __('视频地址','b2'); ?></label>
                                       <input type="url" name="videoLink" class="trix-input trix-input--dialog" id="videoLink">
                                   </div>
                                   <p class="dialog-desc"><?php echo __('复制视频文件地址或各大视频网站视频地址','b2'); ?></p>
                               </div>
                               <div class="trix-dialog-input-box">
                                   <div class="trix-dialog-input">
                                       <label for="videoThumb"><?php echo __('视频封面','b2'); ?></label>
                                       <input type="url" name="videoThumb" class="trix-input trix-input--dialog" id="videoThumb">
                                   </div>
                                   <p class="dialog-desc"><?php echo __('如果不设置封面，程序会自动获取','b2'); ?></p>
                               </div>
                           </div>
                           <div class="trix-dialog-input-box" v-if="videoTable == 'html'" v-cloak>
                               <div class="trix-dialog-input input-textarea">
                                   <textarea type="url" class="trix-input trix-input--dialog" id="videoHtml"></textarea>
                               </div>
                               <p class="dialog-desc"><?php echo __('将内嵌视频代码粘贴在上面','b2'); ?></p>
                           </div>
                           <div class="trix-button-group" v-if="videoTable != 'upload'">
                               <button  class="empty" @click.stop="close('video')"><?php echo __('取消','b2'); ?></button>
                               <button  @click.stop="insert('video')"><?php echo __('插入','b2'); ?></button>
                           </div>
                       </div>
                   </div>
                   <div :class="['trix-dialog trix-dialog--video modal',{'show-modal':showPostBox}]" v-cloak>
                       <div class="trix-dialog__link-fields box">
                           <div class="trix-dialog-title"><?php echo __('插入文章','b2'); ?></div>
                           <span class="close-button" @click.stop="close('post')">×</span>
                           <div class="trix-dialog-input-box">
                               <div class="trix-dialog-input">
                                   <label for="postLink"><?php echo __('文章网址','b2'); ?></label>
                                   <input type="url" name="postLink" class="trix-input trix-input--dialog" id="postLink">
                               </div>
                               <p class="dialog-desc"><?php echo __('只允许插入本站的文章连接','b2'); ?></p>
                           </div>
                           <div class="trix-button-group">
                               <button  @click.stop="close('post')" class="empty"><?php echo __('取消','b2'); ?></button>
                               <button @click.stop="insertPost()"><?php echo __('插入','b2'); ?></button>
                           </div>
                       </div>
                   </div>
                   <div :class="['trix-dialog trix-dialog--video modal',{'show-modal':showFileBox}]" v-cloak>
                       <div class="trix-dialog__link-fields box">
                           <div class="trix-dialog-title"><?php echo __('插入附件','b2'); ?></div>
                           <span class="close-button" @click.stop="close('file')">×</span>
                           <div class="trix-dialog-input-box">
                               <div class="trix-dialog-input">
                                   <label for="fileTitle"><?php echo __('标题','b2'); ?></label>
                                   <input type="url" name="fileTitle" class="trix-input trix-input--dialog" id="fileTitle">
                               </div>
                           </div>
                           <div class="trix-dialog-input-box">
                               <div class="trix-dialog-input">
                                   <label for="fileLink"><?php echo __('连接','b2'); ?></label>
                                   <input type="url" name="fileLink" class="trix-input trix-input--dialog" id="fileLink">
                               </div>
                           </div>
                           <div class="trix-dialog-input-box">
                               <div class="trix-dialog-input">
                                   <label for="fileTq"><?php echo __('提取码','b2'); ?></label>
                                   <input type="url" name="fileTq" class="trix-input trix-input--dialog" id="fileTq">
                               </div>
                               <p class="dialog-desc"><?php echo __('选填','b2'); ?></p>
                           </div>
                           <div class="trix-dialog-input-box">
                               <div class="trix-dialog-input">
                                   <label for="fileJy"><?php echo __('解压码','b2'); ?></label>
                                   <input type="url" name="fileJy" class="trix-input trix-input--dialog" id="fileJy">
                               </div>
                               <p class="dialog-desc"><?php echo __('选填','b2'); ?></p>
                           </div>
                           <div class="trix-button-group">
                               <button @click.stop="close('file')" class="empty"><?php echo __('取消','b2'); ?></button>
                               <button @click.stop="insertFile"><?php echo __('插入','b2'); ?></button>
                           </div>
                       </div>
                   </div>
               </div>
               <div id="write-footer">
                   <div class="write-select-row">
                       <div class="write-select-row-title" @click.stop="showSetting('tag')">
                           <div class="write-title"><?php echo __('标签','b2'); ?></div>
                           <div class="write-select">
                               <?php echo __('选填','b2'); ?>
                           </div>
                       </div>
                       <div class="write-role-settings" v-show="show.tag" v-cloak>
                           <p><?php echo __('请输入标签，最多可以设置4个','b2'); ?></p>
                           <label class="write-tags">
                               <?php echo b2_get_icon('b2-price-tag-3-line'); ?>
                               <div class="cat-picked">
                                   <span v-for="(t,index) in tags" @click.stop="removeTag(index)" v-text="t"></span>
                               </div>
                               <input type="text" @input="tagChange($event)" @keydown="tagChange($event)" @blur="tagChange($event)" v-model="tag" placeholder="<?php echo __('请输入标签','b2'); ?>">
                           </label>
                       </div>
                   </div>
                   <div class="write-select-row">
                       <div class="write-select-row-title" @click.stop="showSetting('excerpt')">
                           <div class="write-title"><?php echo __('摘要','b2'); ?></div>
                           <div class="write-select">
                               <?php echo __('选填','b2'); ?>
                           </div>
                       </div>
                       <div class="write-role-settings" v-show="show.excerpt" v-cloak>
                           <p><?php echo __('请输入描述内容','b2'); ?></p>
                           <div class="write-textarea-box">
                               <textarea v-model="excerpt" ref="excerpt"></textarea>
                           </div>
                       </div>
                   </div>
                   <div class="write-bottom">
                       <div class="mobile-hidden"><?php echo __('请尊重自己和别人的时间，不要发布垃圾和广告内容。','b2'); ?></div>
                       <div>
                           <button @click.stop="submit('draft')" :disabled="locked"><?php echo __('保存草稿到服务器','b2'); ?></button>
                           <button @click.stop="submit('publish')" :disabled="locked"><?php echo __('发布','b2'); ?></button>
                       </div>
                   </div>
               </div>
           </main>
       </div>
       <?php get_template_part( 'Sidebars/sidebar'); ?>
</div>

<?php
get_footer();